﻿using System;
using System.Collections.Generic;

// 定义形状接口
interface IShape
{
    double CalculateArea();
}

// 定义抽象类 Shape
abstract class Shape : IShape
{
    public abstract double CalculateArea();
}

// 长方形类
class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public override double CalculateArea()
    {
        return Width * Height;
    }
}

// 正方形类
class Square : Shape
{
    public double SideLength { get; set; }

    public override double CalculateArea()
    {
        return SideLength * SideLength;
    }
}

// 三角形类
class Triangle : Shape
{
    public double Base { get; set; }
    public double Height { get; set; }

    public override double CalculateArea()
    {
        return 0.5 * Base * Height;
    }
}

// 简单工厂类
class ShapeFactory
{
    public static Shape CreateShape(string shapeType)
    {
        switch (shapeType)
        {
            case "Rectangle":
                return new Rectangle { Width = GetRandomNumber(), Height = GetRandomNumber() };
            case "Square":
                return new Square { SideLength = GetRandomNumber() };
            case "Triangle":
                return new Triangle { Base = GetRandomNumber(), Height = GetRandomNumber() };
            default:
                throw new ArgumentException("Invalid shape type");
        }
    }

    private static double GetRandomNumber()
    {
        Random random = new Random();
        return random.Next(1, 10); // 生成1到10之间的随机数作为形状的边长或底边、高
    }
}

class Program
{
    static void Main()
    {
        List<Shape> shapes = new List<Shape>();
        double totalArea = 0;

        for (int i = 0; i < 10; i++)
        {
            string[] shapeTypes = { "Rectangle", "Square", "Triangle" };
            Random random = new Random();
            string randomShapeType = shapeTypes[random.Next(shapeTypes.Length)];

            Shape shape = ShapeFactory.CreateShape(randomShapeType);
            shapes.Add(shape);
        }

        foreach (var shape in shapes)
        {
            totalArea += shape.CalculateArea();
        }

        Console.WriteLine($"总面积为: {totalArea}");
        Console.ReadKey();
    }
    
}